<template>
	<div class="detail-container">
		<!-- 顶部导航 -->
		<div class="header">
			<div class="back-btn" @click="goBack">
				<text>← 返回</text>
			</div>
			<text class="title">任务详情</text>
			<div class="dialogue-btn" @click="openDialogue" v-if="questDetail.status === 'progress'">
				<text>剧情</text>
			</div>
		</div>

		<!-- 任务基本信息 -->
		<div class="quest-info">
			<div class="quest-header">
				<text class="quest-name">{{ questDetail.name }}</text>
				<div class="quest-meta">
					<text class="quest-type">{{ questDetail.type }}</text>
					<text class="quest-level">Lv.{{ questDetail.level }}</text>
				</div>
			</div>
			<text class="quest-desc">{{ questDetail.description }}</text>
		</div>

		<!-- 任务目标 -->
		<div class="objectives-section">
			<div class="section-title">
				<text>任务目标</text>
			</div>
			<div class="objectives-list">
				<div class="objective-item" 
					  v-for="(objective, index) in questProgress.objectives" 
					  :key="index"
					  :class="{ completed: objective.completed }">
					<div class="objective-checkbox">
						<text v-if="objective.completed">✓</text>
					</div>
					<div class="objective-content">
						<text class="objective-desc">{{ objective.description }}</text>
						<text class="objective-progress">{{ objective.current }}/{{ objective.target }}</text>
					</div>
				</div>
			</div>
		</div>

		<!-- 任务奖励 -->
		<div class="rewards-section">
			<div class="section-title">
				<text>任务奖励</text>
			</div>
			<div class="rewards-grid">
				<div class="reward-card">
					<text class="reward-icon">💰</text>
					<text class="reward-label">银两</text>
					<text class="reward-value">{{ questDetail.rewards.coins }}</text>
				</div>
				<div class="reward-card">
					<text class="reward-icon">⭐</text>
					<text class="reward-label">经验</text>
					<text class="reward-value">{{ questDetail.rewards.exp }}</text>
				</div>
				<div class="reward-card" v-if="questDetail.rewards.skill_point">
					<text class="reward-icon">📚</text>
					<text class="reward-label">武学点</text>
					<text class="reward-value">{{ questDetail.rewards.skill_point }}</text>
				</div>
				<div class="reward-card" v-if="questDetail.rewards.attribute_points">
					<text class="reward-icon">💪</text>
					<text class="reward-label">属性点</text>
					<text class="reward-value">{{ questDetail.rewards.attribute_points }}</text>
				</div>
			</div>
			<div class="reward-items" v-if="questDetail.rewards.items && questDetail.rewards.items.length > 0">
				<div class="item-card" v-for="item in questDetail.rewards.items" :key="item.id">
					<image class="item-icon" :src="getItemIcon(item.icon)" mode="aspectFit"></image>
					<text class="item-name">{{ item.name }}</text>
					<text class="item-count">×{{ item.count }}</text>
				</div>
			</div>
		</div>

		<!-- 任务进度 -->
		<div class="progress-section" v-if="questDetail.status === 'progress'">
			<div class="section-title">
				<text>完成进度</text>
			</div>
			<div class="progress-display">
				<div class="progress-bar">
					<div class="progress-fill" :style="{ width: (questProgress.progress * 100) + '%' }"></div>
				</div>
				<text class="progress-text">{{ Math.floor(questProgress.progress * 100) }}%</text>
			</div>
		</div>

		<!-- 操作按钮 -->
		<div class="action-buttons">
			<div class="submit-btn" 
				  v-if="questDetail.status === 'progress' && questProgress.progress >= 1.0"
				  @click="submitQuest">
				<text>提交任务</text>
			</div>
			<div class="track-btn" 
				  v-if="questDetail.status === 'progress'"
				  @click="trackQuest">
				<text>追踪任务</text>
			</div>
			<div class="map-btn" @click="openMap">
				<text>查看地图</text>
			</div>
		</div>

		<!-- NPC对话区 -->
		<div class="npc-section" v-if="npcInfo">
			<div class="section-title">
				<text>相关NPC</text>
			</div>
			<div class="npc-card" @click="talkToNPC">
				<image class="npc-avatar" :src="getNPCAvatar(npcInfo.avatar)" mode="aspectFit"></image>
				<div class="npc-info">
					<text class="npc-name">{{ npcInfo.name }}</text>
					<text class="npc-desc">{{ npcInfo.description }}</text>
				</div>
				<div class="talk-btn">
					<text>对话</text>
				</div>
			</div>
		</div>

		<!-- 加载状态 -->
		<div class="loading" v-if="loading">
			<text>加载中...</text>
		</div>
	</div>
</template>

<script>
export default {
	data() {
		return {
			questId: '',
			questDetail: {
				id: '',
				name: '',
				type: '',
				level: 0,
				status: '',
				description: '',
				rewards: {}
			},
			questProgress: {
				progress: 0,
				status: '',
				objectives: []
			},
			npcInfo: null,
			loading: false
		}
	},
	onLoad(options) {
		this.questId = options.id || ''
		if (this.questId) {
			this.loadQuestDetail()
			this.loadQuestProgress()
		}
	},
	methods: {
		// 加载任务详情
		async loadQuestDetail() {
			this.loading = true
			try {
				// 模拟任务详情数据
				this.questDetail = {
					id: this.questId,
					name: '探寻武穆遗书',
					type: '主线',
					level: 35,
					status: 'progress',
					description: '前往铁掌峰，从裘千仞手中夺回武穆遗书。武穆遗书是岳飞留下的兵法秘籍，对于抗金大业具有重要意义。',
					rewards: {
						exp: 5000,
						coins: 1000,
						items: [
							{id: '201', name: '武穆遗书残页', count: 1, icon: 'wumu.svg'}
						]
					}
				}

				// 模拟NPC信息
				this.npcInfo = {
					name: '黄蓉',
					avatar: 'huangrong.svg',
					description: '桃花岛主之女，聪明伶俐，武功高强'
				}
			} catch (error) {
				console.error('加载任务详情失败:', error)
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},

		// 加载任务进度
		async loadQuestProgress() {
			try {
				const response = await uni.request({
					url: `/api/quest/progress/${this.questId}`,
					method: 'GET',
					data: {
						user_id: 'player_001'
					}
				})

				if (response.data.code === 200) {
					this.questProgress = response.data.data
				}
			} catch (error) {
				console.error('加载任务进度失败:', error)
			}
		},

		// 提交任务
		async submitQuest() {
			try {
				const response = await uni.request({
					url: '/api/quest/submit',
					method: 'POST',
					data: {
						user_id: 'player_001',
						quest_id: this.questId
					}
				})

				if (response.data.code === 200) {
					uni.showModal({
						title: '任务完成',
						content: '恭喜完成任务！获得奖励已发放到背包。',
						showCancel: false,
						success: () => {
							uni.navigateBack()
						}
					})
				} else {
					uni.showToast({
						title: response.data.message || '提交失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('提交任务失败:', error)
				uni.showToast({
					title: '网络错误',
					icon: 'none'
				})
			}
		},

		// 追踪任务
		trackQuest() {
			uni.showToast({
				title: '任务追踪已开启',
				icon: 'success'
			})
		},

		// 打开剧情对话
		openDialogue() {
			uni.navigateTo({
				url: `/pages/quest/dialogue?quest_id=${this.questId}&step=1`
			})
		},

		// 打开地图
		openMap() {
			uni.navigateTo({
				url: '/pages/quest/map'
			})
		},

		// 与NPC对话
		talkToNPC() {
			this.openDialogue()
		},

		// 返回上一页
		goBack() {
			uni.navigateBack()
		},

		// 获取物品图标
		getItemIcon(icon) {
			return `/static/items/${icon}`
		},

		// 获取NPC头像
		getNPCAvatar(avatar) {
			return `/static/avatars/${avatar}`
		}
	}
}
</script>

<style scoped>
.detail-container {
	padding: 20rpx;
	background-color: #f5f5f5;
	min-height: 100vh;
}

.header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
	padding: 20rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 10rpx;
	color: white;
}

.back-btn, .dialogue-btn {
	font-size: 28rpx;
	padding: 10rpx 20rpx;
	border-radius: 8rpx;
	background-color: rgba(255,255,255,0.2);
}

.title {
	font-size: 36rpx;
	font-weight: bold;
}

.quest-info {
	background-color: white;
	border-radius: 15rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.08);
}

.quest-header {
	margin-bottom: 15rpx;
}

.quest-name {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
}

.quest-meta {
	display: flex;
	gap: 15rpx;
}

.quest-type {
	font-size: 22rpx;
	padding: 4rpx 12rpx;
	background-color: #f0f0f0;
	border-radius: 10rpx;
	color: #666;
}

.quest-level {
	font-size: 22rpx;
	padding: 4rpx 12rpx;
	background-color: #e6f7ff;
	border-radius: 10rpx;
	color: #1890ff;
}

.quest-desc {
	font-size: 28rpx;
	color: #666;
	line-height: 1.6;
}

.objectives-section, .rewards-section, .progress-section, .npc-section {
	background-color: white;
	border-radius: 15rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.08);
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 20rpx;
	padding-bottom: 10rpx;
	border-bottom: 2rpx solid #f0f0f0;
}

.objectives-list {
	display: flex;
	flex-direction: column;
	gap: 15rpx;
}

.objective-item {
	display: flex;
	align-items: center;
	padding: 20rpx;
	background-color: #f8f9fa;
	border-radius: 12rpx;
	transition: all 0.3s;
}

.objective-item.completed {
	background: linear-gradient(135deg, #e6f7ff 0%, #bae7ff 100%);
}

.objective-checkbox {
	width: 40rpx;
	height: 40rpx;
	border: 3rpx solid #ccc;
	border-radius: 8rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-right: 20rpx;
	transition: all 0.3s;
}

.objective-item.completed .objective-checkbox {
	background-color: #52c41a;
	border-color: #52c41a;
	color: white;
}

.objective-content {
	flex: 1;
}

.objective-desc {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 5rpx;
	display: block;
}

.objective-progress {
	font-size: 24rpx;
	color: #666;
	font-weight: bold;
}

.rewards-grid {
	display: grid;
	grid-template-columns: repeat(2, 1fr);
	gap: 15rpx;
	margin-bottom: 20rpx;
}

.reward-card {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 25rpx;
	background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
	border-radius: 12rpx;
}

.reward-icon {
	font-size: 40rpx;
	margin-bottom: 10rpx;
}

.reward-label {
	font-size: 22rpx;
	color: #666;
	margin-bottom: 5rpx;
}

.reward-value {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
}

.reward-items {
	display: flex;
	flex-wrap: wrap;
	gap: 15rpx;
}

.item-card {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 20rpx;
	background-color: #f8f9fa;
	border-radius: 12rpx;
	min-width: 120rpx;
}

.item-icon {
	width: 60rpx;
	height: 60rpx;
	margin-bottom: 10rpx;
}

.item-name {
	font-size: 22rpx;
	color: #333;
	margin-bottom: 5rpx;
	text-align: center;
}

.item-count {
	font-size: 20rpx;
	color: #666;
	font-weight: bold;
}

.progress-display {
	display: flex;
	align-items: center;
	gap: 20rpx;
}

.progress-bar {
	flex: 1;
	height: 20rpx;
	background-color: #f0f0f0;
	border-radius: 10rpx;
	overflow: hidden;
}

.progress-fill {
	height: 100%;
	background: linear-gradient(90deg, #1890ff 0%, #52c41a 100%);
	border-radius: 10rpx;
	transition: width 0.3s;
}

.progress-text {
	font-size: 28rpx;
	color: #333;
	font-weight: bold;
}

.action-buttons {
	display: flex;
	gap: 15rpx;
	margin-bottom: 20rpx;
}

.submit-btn, .track-btn, .map-btn {
	flex: 1;
	padding: 25rpx;
	border-radius: 12rpx;
	text-align: center;
	transition: transform 0.2s;
}

.submit-btn {
	background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
	color: white;
}

.track-btn {
	background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
	color: white;
}

.map-btn {
	background: linear-gradient(135deg, #fa8c16 0%, #ffa940 100%);
	color: white;
}

.submit-btn:active, .track-btn:active, .map-btn:active {
	transform: scale(0.95);
}

.submit-btn text, .track-btn text, .map-btn text {
	font-size: 28rpx;
	font-weight: bold;
}

.npc-card {
	display: flex;
	align-items: center;
	padding: 20rpx;
	background-color: #f8f9fa;
	border-radius: 12rpx;
	transition: background-color 0.2s;
}

.npc-card:active {
	background-color: #e9ecef;
}

.npc-avatar {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	margin-right: 20rpx;
}

.npc-info {
	flex: 1;
}

.npc-name {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 5rpx;
	display: block;
}

.npc-desc {
	font-size: 24rpx;
	color: #666;
}

.talk-btn {
	padding: 12rpx 24rpx;
	background-color: #1890ff;
	border-radius: 20rpx;
}

.talk-btn text {
	font-size: 24rpx;
	color: white;
	font-weight: bold;
}

.loading {
	text-align: center;
	padding: 40rpx;
	color: #666;
}
</style>